package com.foftact.etfposition.controller;

import com.foftact.etfposition.ETFBlock;
import com.foftact.etfposition.service.ETFBlockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ETF行业配置控制器
 */
@Controller
@RequestMapping("/etfblock")
public class ETFBlockController {
    private static final Logger logger = LoggerFactory.getLogger(ETFBlockController.class);
    
    @Autowired
    private ETFBlockService etfBlockService;
    
    /**
     * 跳转到ETF行业配置首页
     */
    @RequestMapping("/index")
    public String index() {
        return "etfposition/index";
    }
    
    /**
     * 抓取并保存ETF行业配置数据
     */
    @RequestMapping("/fetch")
    @ResponseBody
    public Map<String, Object> fetchData() {
        logger.info("接收到抓取ETF行业配置数据请求");
        try {
            int count = etfBlockService.fetchAndSaveETFBlockData();
            return Map.of("success", true, "message", "成功抓取" + count + "条数据");
        } catch (Exception e) {
            logger.error("抓取数据失败", e);
            return Map.of("success", false, "message", "抓取数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据报告日期获取ETF行业配置数据
     */
    @RequestMapping("/listByDate")
    @ResponseBody
    public List<ETFBlock> getListByDate(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date reportDate) {
        logger.info("根据报告日期查询ETF行业配置数据: {}", reportDate);
        return etfBlockService.getETFBlocksByReportDate(reportDate);
    }
    
    /**
     * 根据日期范围获取ETF行业配置数据
     */
    @RequestMapping("/listByDateRange")
    @ResponseBody
    public List<ETFBlock> getListByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        logger.info("根据日期范围查询ETF行业配置数据: {} 至 {}", startDate, endDate);
        return etfBlockService.getETFBlocksByDateRange(startDate, endDate);
    }
    
    /**
     * 获取所有ETF行业配置数据
     */
    @RequestMapping("/listAll")
    @ResponseBody
    public List<ETFBlock> listAll() {
        logger.info("查询所有ETF行业配置数据");
        return etfBlockService.getAllETFBlocks();
    }
    
    /**
     * 保存ETF行业配置数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(@RequestBody ETFBlock etfBlock) {
        logger.info("保存ETF行业配置数据: {}", etfBlock);
        try {
            int count = etfBlockService.saveETFBlock(etfBlock);
            return Map.of("success", true, "message", "保存成功", "count", count);
        } catch (Exception e) {
            logger.error("保存数据失败", e);
            return Map.of("success", false, "message", "保存失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除旧数据
     */
    @RequestMapping("/deleteOld")
    @ResponseBody
    public Map<String, Object> deleteOldData(@RequestParam int days) {
        logger.info("删除{}天前的ETF行业配置数据", days);
        try {
            int count = etfBlockService.deleteOldData(days);
            return Map.of("success", true, "message", "成功删除" + count + "条旧数据");
        } catch (Exception e) {
            logger.error("删除旧数据失败", e);
            return Map.of("success", false, "message", "删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 计算并更新ETF行业配置比例
     */
    @RequestMapping("/calculate")
    @ResponseBody
    public Map<String, Object> calculateBlockRatio(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date reportDate) {
        logger.info("计算并更新ETF行业配置比例，报告日期: {}", reportDate);
        try {
            int count = etfBlockService.calculateAndUpdateBlockRatio(reportDate);
            return Map.of("success", true, "message", "成功计算并更新" + count + "条数据");
        } catch (Exception e) {
            logger.error("计算行业配置比例失败", e);
            return Map.of("success", false, "message", "计算失败: " + e.getMessage());
        }
    }
}